package revontuli.aMHD;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class UserLocationSvc extends Service {
	// objekty na spojenie aktivit a sluzby
	private Messenger mClient = null;
	private final IBinder mBinder = new ClientBinder();

	// objekty s GPS udajmi
	private int mRange = aMHDApp.MIN_SEARCH_RANGE;
	private Location mLoc;
	private LocationManager mLocManager;
	private LocationListener mLocListener = new LocationListener() {
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		@Override
		public void onProviderEnabled(String provider) {
		}
		@Override
		public void onProviderDisabled(String provider) {
		}
		@Override
		public void onLocationChanged(Location location) {
			mLoc = location;
			if (mClient != null) {
				try {
					mClient.send(Message.obtain(null, aMHDApp.MSG_REFRESH_GPS));
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	};

	public class ClientBinder extends Binder {
		public UserLocationSvc getService() {
			return UserLocationSvc.this;
		}
	}
	
	public void registerMessenger(Messenger msgr) {
		mClient = msgr;
	}

	@Override
	public IBinder onBind(Intent intent) {
		startUpdates();
		return mBinder;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		stopUpdates();
		aMHDApp.mDataManager.close();
		return false;
	}
	
	/**
	 * Vracia poziciu
	 * @return 
	 */
	public Location getLocation() {
		return this.mLoc;
	}
	
	/**
	 * Nastavuje okruh vyhladavania v statickej triede
	 * @param range
	 */
	public void setRange(int range) {
		this.mRange = range;
	}
	
	/**
	 * Vracia okruh vyhladavania
	 * @return
	 */
	public int getRange() {
		return this.mRange;
	}
	
	/**
	 * Zapne GPS
	 */
	private void startUpdates() {
		mLocManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
		if (mLocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null) {
			mLoc = mLocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		} else if (mLocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) != null) {
			mLoc = mLocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		} else {
			mLoc = new Location("");
		}
		mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, mLocListener);
		mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, mLocListener);
	}
	
	/**
	 * Vypne GPS
	 */
	private void stopUpdates() {
		mLocManager.removeUpdates(mLocListener);
	}


}
